<template>
	<div class="cms-site-container">
		<el-dialog title="修改栏目" v-model="isShowDialog" >
		<el-form ref="configSiteFormRef" :model="formData" :rules="formRules" label-width="80px">
			<el-row :gutter="35">
				<el-col>
					<el-form-item label="站点地址" prop="siteUrl">
						<el-input v-model="formData.siteUrl" placeholder="请输入站点地址" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col>
					<el-form-item label="站点编码" prop="siteCode">
						<el-input v-model="formData.siteCode" placeholder="请输入站点编码" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col>
					<el-form-item label="站点备注" prop="remark">
						<el-input type="textarea" v-model="formData.remark" placeholder="请输入站点备注" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel">取 消</el-button>
					<el-button type="primary" @click="onSubmit">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref, defineComponent } from 'vue';
import { getSiteConfig, configSite } from '/@/api/cms/channel/index';
export default defineComponent ({
	name: 'cms/site/config',
	setup(props) {
		const configSiteFormRef = ref();
		const state = reactive({
			isShowDialog: false,
			verifyToken: '',
			formData: {
				siteId: '',
				siteUrl: '',
				siteCode: '',
				remark: ''
			},
			formRules : {
				siteUrl : { required: true, message: '站点地址不能为空', trigger: 'blur' },
				siteCode: { required: true, message: '站点编码不能为空', trigger: 'blur' },
			},
		});
		
		const loadSiteConfig = (siteId: string) => {
			getSiteConfig({'siteId': siteId}).then((res) => {
				if(res.code == 0 && res.data){
					state.formData = res.data;
				}
			});
		}
		// 打开弹窗
		const openDialog = (row?: object) => {
			if(row){
				state.isShowDialog = true;
				state.formData.siteId = row.siteId;
				state.verifyToken = row.verifyToken;
				loadSiteConfig(state.formData.siteId);
			}
		};
		// 关闭弹窗
		const closeDialog = (row?: object) => {
			state.isShowDialog = false;
			state.verifyToken = '';
			for(var key in state.formData) {
				state.formData[key] = undefined;
			}
			configSiteFormRef.value.resetFields();
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 保存
		const onSubmit = () => {
			configSiteFormRef.value.validate(valid => {
				if(valid){
					let params = {
						id: state.formData.id,
						siteId: state.formData.siteId,
						siteUrl: state.formData.siteUrl,
						siteCode: state.formData.siteCode,
						remark: state.formData.remark
					}
					configSite(state.verifyToken, params).then((res) => {
						if(res.code === 0){
							closeDialog();
						}
					});
				}
			});
		};
		return {
			configSiteFormRef,
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
});
</script>

<style>
</style>
